package com.example.studyplanning.controller;

import com.example.studyplanning.entity.Focus;
import com.example.studyplanning.services.FocusServices;
import com.example.studyplanning.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("focus")
public class FocusController {
    @Autowired
    private FocusServices focusServices;

    //收藏心得
    @PostMapping("/add")
    public Result InsertFocus(@RequestBody Focus focus){
        Result result = new Result();
        int i = focusServices.InsertFocus(focus);
        if(i==1){
            result.setCode(200);
            result.setMessage("收藏成功");
        }else{
            result.setCode(400);
            result.setMessage("收藏失败");
        }
        return result;
    };

    //展示收藏夹
    @GetMapping("/list")
    public Result findAllFocus(@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(focusServices.findAllFocus(userID));
        return result;
    };

    //展示是否已收藏
    @GetMapping("/lists")
    public Result FindFocus(@RequestParam("userID") Integer userID,
                           @RequestParam("tipsID") Integer tipsID){
        Result result = new Result();
        Integer i  = focusServices.FindFocus(userID,tipsID);
        if(i==0){
        result.setCode(400);
        result.setMessage("未收藏");}
        else {
            result.setCode(200);
            result.setMessage("已收藏");
        }
        return result;
    };

    //取消收藏
    @GetMapping("/del")
    public Result DelFocus(@RequestParam("tipsID") Integer tipsID){
        Result result = new Result();
        int i = focusServices.DelFocus(tipsID);
        if (i==1){
            result.setCode(200);
            result.setMessage("删除成功");
        }else {
            result.setCode(400);
            result.setMessage("删除失败");
        }
        return result;
    };

}
